﻿#include "CPointMgrBase.h"

CPointMgrBase::CPointMgrBase(CDevDataBase * pDevData, CRBaseData * pRBaseData)
{
	m_pTimer = new QTimer(this);
	m_pRBaseData = pRBaseData;
	m_pDevData = pDevData;
	createPoints();
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &CPointMgrBase::refreshData);
	start();
}


CPointMgrBase::~CPointMgrBase()
{
	if (m_pTimer != NULL)
	{
		if (m_pTimer->isActive())
		{
			m_pTimer->stop();
		}
		delete m_pTimer;
		m_pTimer = NULL;
	}
}

void CPointMgrBase::start()
{
	if (!m_pTimer->isActive())
	{
		m_pTimer->start(1000);
	}
}

void CPointMgrBase::stop()
{
	if (m_pTimer->isActive())
	{
		m_pTimer->stop();
	}
}

QList<CMeasurePoint*> CPointMgrBase::getPoint()
{
	return m_vecPoints;
}

void CPointMgrBase::createPoints()
{
	CMeasurePoint *pDevState = new CMeasurePoint("DevState", A2T("设备状态"), "", NULL, EPointType::enBitMask);
	CMeasurePoint *pRobBatteryInfo = new CMeasurePoint("RobBatteryInfo", A2T("机器人电量"), "", NULL, EPointType::enAnalog);
	CMeasurePoint *pRobCurrLoca = new CMeasurePoint("RobCurrLoca", A2T("机器人位置"), "", NULL, EPointType::enAnalog,"m");
	CMeasurePoint *pPlcConnect = new CMeasurePoint("PlcConnect", A2T("PLC连接状态"), "", NULL, EPointType::enDigital);
	CMeasurePoint *pRobWorkStatus = new CMeasurePoint("RobWorkStatus", A2T("机器人工作状态"), "", NULL, EPointType::enEnum);
	CMeasurePoint *pRobAlarmStatus = new CMeasurePoint("RobAlarmStatus", A2T("设备报警状态"), "", NULL, EPointType::enEnum);
	CMeasurePoint *pBatteryVol = new CMeasurePoint("BatteryVol", A2T("电池电压"), "", NULL, EPointType::enAnalog,"V");
	CMeasurePoint *pBatteryCurr = new CMeasurePoint("BatteryCurr", A2T("电池电流"), "", NULL, EPointType::enAnalog,"A");
	CMeasurePoint *pAtcTempMax = new CAlarmPoint("AtcTempMax", A2T("红外温度"), NULL, EPointType::enAnalog,A2T("℃"),-20,0,80,100);
	CMeasurePoint *pdAtcDecibel = new CAlarmPoint("AtcDecibel", A2T("声音"), NULL, EPointType::enAnalog, "db",0,0,110,130);
	CMeasurePoint* pDetecInfo = new CMeasurePoint("DetecInfo", A2T("检测位置"),"",NULL, EPointType::enString);
	CMeasurePoint* pRollerNo = new CMeasurePoint("RollerNo", A2T("当前托辊号"), "", NULL, EPointType::enAnalog);
	CMeasurePoint* pRollerName = new CMeasurePoint("RollerName", A2T("当前托辊名称"), "", NULL, EPointType::enString);
	m_vecPoints.push_back(pDevState);
	m_vecPoints.push_back(pRobBatteryInfo);
	m_vecPoints.push_back(pRobCurrLoca);
	m_vecPoints.push_back(pPlcConnect);
	m_vecPoints.push_back(pRobWorkStatus);
	m_vecPoints.push_back(pRobAlarmStatus);
	m_vecPoints.push_back(pBatteryVol);
	m_vecPoints.push_back(pBatteryCurr);
	m_vecPoints.push_back(pAtcTempMax);
	m_vecPoints.push_back(pdAtcDecibel);
	m_vecPoints.push_back(pDetecInfo);
	m_vecPoints.push_back(pRollerNo);
	m_vecPoints.push_back(pRollerName);
}

void CPointMgrBase::refreshData()
{
	/**
	* @ Author: Gaojia
	* @ Description: 刷新测点数据
	*/
	if (m_pDevData->lock(50))
	{
		m_vecPoints[0]->setValue(m_pDevData->m_sDevWorkStatus.toInt());
		m_vecPoints[1]->setValue(m_pDevData->m_sRobBatteryInfo.toDouble());
		m_vecPoints[2]->setValue(m_pDevData->m_sRobCurrLoca.toDouble() + m_pDevData->m_dDetecOffset * 1000);
		m_vecPoints[3]->setValue(m_pDevData->m_bPlcConnectState);
		m_vecPoints[4]->setValue(m_pDevData->m_sDevWorkStatus.toInt());
		m_vecPoints[5]->setValue(m_pDevData->m_sDevStatus.toInt());
		m_vecPoints[6]->setValue(m_pDevData->m_sBatteryVol.toDouble());
		m_vecPoints[7]->setValue(m_pDevData->m_sBatteryCurr.toDouble());
		m_vecPoints[8]->setValue(m_pDevData->m_pDevDataCam->m_dAtcTempMax);
		m_vecPoints[9]->setValue(m_pDevData->m_pDevDataCam->m_dAtcDecibel);
		m_vecPoints[10]->setValue(QString::number(m_pDevData->m_nBelogn));
		m_pDevData->unLock();
	}

}
